﻿Public Class clsLibrary

    Private c_Name As String
    Private c_Topgroups As List(Of clsTopGroup)
    Private c_VisibleLines As List(Of Object)

    Public Property Name() As String
        Get
            Return c_Name
        End Get
        Set(ByVal value As String)
            c_Name = value
        End Set
    End Property
    
    Public Property Topgroups() As List(Of clsTopGroup)
        Get
            Return c_Topgroups
        End Get
        Set(ByVal value As List(Of clsTopGroup))
            c_Topgroups = value
        End Set
    End Property

    Public ReadOnly Property SelectedTopGroup() As Int32
        Get
            Dim idxSelected As Int32 = -1
            For idx = 0 To c_Topgroups.Count - 1
                If c_Topgroups(idx).IsSelected Then
                    idxSelected = idx
                    Exit For
                End If
            Next
        End Get
    End Property

    Public ReadOnly Property SelectedItem() As Object
        Get
            For Each objTopGroup As clsTopGroup In c_Topgroups
                If objTopGroup.IsSelected Then
                    Return objTopGroup
                End If
                For Each objSubGroup As clsSubGroup In objTopGroup.Subgroups
                    If objSubGroup.IsSelected Then
                        Return objSubGroup
                    End If
                    For Each objInstrument As clsInstrument In objSubGroup.Instruments
                        If objInstrument.IsSelected Then
                            Return objInstrument
                        End If
                    Next
                Next
            Next
        End Get
    End Property

    Public ReadOnly Property VisibleLines() As List(Of Object)
        Get
            Return GetVisibleLines()
        End Get
    End Property

    Private Function GetVisibleLines() As List(Of Object)

        Dim lines As New List(Of Object)

        For Each objTopGroup As clsTopGroup In c_Topgroups
            lines.Add(objTopGroup)
            If objTopGroup.IsExpanded Then
                For Each objSubGroup As clsSubGroup In objTopGroup.Subgroups
                    lines.Add(objSubGroup)
                    If objSubGroup.IsExpanded Then
                        For Each objInstrument As clsInstrument In objSubGroup.Instruments
                            lines.Add(objInstrument)
                        Next
                    End If
                Next
            End If
        Next

        Return lines

    End Function
End Class
